package public

import (
	"encoding/json"
	"gitcode.com/G-YT/iHealth25/server/global"
	"gitcode.com/G-YT/iHealth25/server/model/common/request"
	"gitcode.com/G-YT/iHealth25/server/model/common/response"
	"gitcode.com/G-YT/iHealth25/server/model/public"
	publicReq "gitcode.com/G-YT/iHealth25/server/model/public/request"
	"gitcode.com/G-YT/iHealth25/server/service"
	"gitcode.com/G-YT/iHealth25/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type MessageApi struct {
}

var messageService = service.ServiceGroupApp.PublicServiceGroup.MessageService

// CreateMessage 创建消息
// @Tags Message
// @Summary 创建消息
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body public.Message true "创建消息"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /message/createMessage [post]
func (messageApi *MessageApi) CreateMessage(c *gin.Context) {
	var message public.SystemMessage
	err := c.ShouldBindJSON(&message)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	message.Type = "system"

	var infoMessage public.Message

	tb, err := json.Marshal(map[string]interface{}{
		"info": message.Info,
	})

	infoMessage.To = message.To
	infoMessage.Type = message.Type
	infoMessage.Info = tb

	if err := messageService.CreateMessage(&infoMessage); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteMessage 删除消息
// @Tags Message
// @Summary 删除消息
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body public.Message true "删除消息"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /message/deleteMessage [delete]
func (messageApi *MessageApi) DeleteMessage(c *gin.Context) {
	var message public.Message
	err := c.ShouldBindJSON(&message)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := messageService.DeleteMessage(message); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteMessageByIds 批量删除消息
// @Tags Message
// @Summary 批量删除消息
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除消息"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /message/deleteMessageByIds [delete]
func (messageApi *MessageApi) DeleteMessageByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := messageService.DeleteMessageByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateMessage 更新消息
// @Tags Message
// @Summary 更新消息
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body public.Message true "更新消息"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /message/updateMessage [put]
func (messageApi *MessageApi) UpdateMessage(c *gin.Context) {
	var message public.Message
	err := c.ShouldBindJSON(&message)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := messageService.UpdateMessage(message); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindMessage 用id查询消息
// @Tags Message
// @Summary 用id查询消息
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query public.Message true "用id查询消息"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /message/findMessage [get]
func (messageApi *MessageApi) FindMessage(c *gin.Context) {
	var message public.Message
	err := c.ShouldBindQuery(&message)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if remessage, err := messageService.GetMessage(message.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"remessage": remessage}, c)
	}
}

func (messageApi *MessageApi) GetUserMessage(c *gin.Context) {
	var message public.Message
	err := c.ShouldBindQuery(&message)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	userID := utils.GetUserID(c)
	if remessage, err := messageService.GetUserMessage(message.ID, userID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"remessage": remessage}, c)
	}
}

// GetMessageList 分页获取消息列表
// @Tags Message
// @Summary 分页获取消息列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query publicReq.MessageSearch true "分页获取消息列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /message/getMessageList [get]
func (messageApi *MessageApi) GetMessageList(c *gin.Context) {
	var pageInfo publicReq.MessageSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := messageService.GetMessageInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

func (messageApi *MessageApi) GetUserMessageInfoList(c *gin.Context) {
	var pageInfo publicReq.MessageSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	userID := utils.GetUserID(c)
	userAuth := utils.GetUserAuthorityId(c)
	if list, total, err := messageService.GetUserMessageInfoList(pageInfo, userID, userAuth); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
